<?php

namespace App\Models\Entity\Assets;

use Eloquent as Model;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Common\EntityData\Channel\Sp;

/**
 * Class Assets
 * @package App\Models\Entity\Assets
 * @version July 4, 2018, 5:19 pm CST
 *
 * @property string spid
 * @property string asset_type
 * @property string spasset_id
 */
class Assets extends Model
{
    use SoftDeletes;

    protected $connection = 'xiaole_assets';

    public $table = 'assets';
    

    protected $dates = ['deleted_at'];

    public static $asset_type = array(
        'asset'=>'视频',
        'aasset'=>'音频',
        'lvasset'=>'直播视频',
        'laasset'=>'直播音频'
    );

    public $fillable = [
        'spid',
        'asset_type',
        'spasset_id',
        'spassetkit_id',
        'title',
        'subtitle',
        'short_desc',
        'long_desc',
        'vcat_id',
        'pid',
        'subvcat_id',
        'directors',
        'actors',
        'vlang',
        'serials',
        'sp_file_id',
        'file_url',
        'duration',
        'vformat',
        'tags',
        'resolutions',
        'vbitate',
        'mediatype',
        'pic0',
        'pic1',
        'pic2',
        'pic3',
        'pic4',
        'pic5',
        'pinyin',
        'score',
        'varea',
        'vyears',
        'episode',
        'upto',
        'status'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'spid' => 'string',
        'asset_type' => 'string',
        'spasset_id' => 'string',
        'spassetkit_id' => 'string',
        'title' => 'string',
        'subtitle' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'spid' => 'required|max:50',
        'spasset_id' => 'required'
    ];


    public function stream()
    {
        return $this->hasMany('App\Models\Entity\Assets\AssetStream','asset_id');
    }


    /**
     * 限制查询只包含用户所属SP媒资信息。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSpid($query)
    {
        if(!Admin::user()->isAdministrator()){
            $sp = Sp::find(Admin::user()->sp_id);
            return $query->where('spid', $sp->sp_no);
        }
    }

    
}
